package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import Controller.ComplaintSubmissionController;
import Controller.DBcontroller;
import Entity.Complaint;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;

/**
 * this is the GUI for viewin the complaint back to the customer.
 * @author Moran Abilea
 */
public class CustomerComplaintViewBoundary extends JFrame {

	private JPanel contentPane;
	private JTable table;
	/**
	 * is the constructor. it only shows the table from the DB and also has a button for going back to the menu.
	 */
	public CustomerComplaintViewBoundary(Complaint[] myc) {
		setTitle("Customer Complaints View Window");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 825, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCustomerComplaintsViewing = new JLabel("Customer Complaints viewing");
		lblCustomerComplaintsViewing.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCustomerComplaintsViewing.setBounds(304, 11, 201, 17);
		contentPane.add(lblCustomerComplaintsViewing);
		
		JLabel lblCusomterIdautomatic = new JLabel("Cusomter Id: "+DBcontroller.getLoggedIn());
		lblCusomterIdautomatic.setBounds(10, 37, 337, 14);
		contentPane.add(lblCusomterIdautomatic);
		
		// don't forget to run a SQL query in order to make the JTable visible.
		DBcontroller.viewCustomerComplaintSQL(DBcontroller.getLoggedIn());
		
		JLabel lblYourComplaints = new JLabel("Your complaints");
		lblYourComplaints.setBounds(304, 54, 201, 14);
		contentPane.add(lblYourComplaints);
		
		JButton btnGoBackTo = new JButton("Go back to the menu");
		btnGoBackTo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// this will be the place for going back in the panels (go back to the customer info window).
				close();
				CustomerGUI goback=new CustomerGUI();
				goback.setVisible(true);
			}
		});
		btnGoBackTo.setBounds(52, 230, 191, 23);
		contentPane.add(btnGoBackTo);
		
		// building the table
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(23, 76, 776, 149);
		contentPane.add(scrollPane);
		table = new JTable(new newJTable(ComplaintSubmissionController.getCustomerComplaints(DBcontroller.getLoggedIn())));
		scrollPane.setViewportView(table);
		
	}
	
	public void close()
	{
		WindowEvent windowclose=new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowclose);
	}
}

class newJTable extends AbstractTableModel
{
	String[] titles={"Complaint Num","Submitssion Date","Description","Status","Answer","Answering Date","Compensation"};
	Object[][] data;
	
	public newJTable(Complaint[] c)
	{
		super();
		this.data=new Object[c.length][7];
		for(int i=0; i<c.length; i++)
		{
			this.data[i][0]=c[i].getComplaint_number();
			if(c[i].isHandled())
				this.data[i][3]="Handled";
			else
				this.data[i][3]="Not Handled";
			this.data[i][1]=c[i].getDateOfSubmition();
			this.data[i][2]=c[i].getDescription();
			this.data[i][4]=c[i].getResult();
			this.data[i][5]=c[i].getDateOfAnswer();
			this.data[i][6]=c[i].getCompensation();
		}
	}
	// override the other function.
	@Override
	public int getColumnCount() {return this.titles.length;}

	@Override
	public int getRowCount() {return data.length;}

	@Override
	public Object getValueAt(int arg0, int arg1) {return data[arg0][arg1];}
	
	@Override
	public String getColumnName(int column) {return (String) this.titles[column];}
}